package br.org.groupware_workbench.collabElement.coop.recommendMgr.api;

import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import br.org.groupware_workbench.collablet.news.News;
import br.org.groupware_workbench.coreutils.AssignedTo;
import br.org.groupware_workbench.coreutils.GenericEntity;
import br.usp.mac5855.AnalisadorTextual;
import br.usp.mac5855.exception.AnalisadorTextualException;

import com.alag.ci.textanalysis.TagMagnitudeVector;

@Entity
@Table(name="gw_collab_Recommend")
public class Recommend extends GenericEntity {

    private static final long serialVersionUID = -3399306816323250833L;

    @ManyToOne
    @AssignedTo
    private GenericEntity genericEntityFrom;

    @ManyToOne
    @AssignedTo
    private GenericEntity genericEntityTo;

    private double similaritysProduct;

    public Recommend() {
    }

    public Recommend(GenericEntity genericEntityFrom, GenericEntity genericEntityTo) {
        this.genericEntityFrom = genericEntityFrom;
        this.genericEntityTo = genericEntityTo;
        this.similaritysProduct = getSimilarity(genericEntityFrom, genericEntityTo);
    }

    public GenericEntity getGenericEntityFrom() {
        return genericEntityFrom;
    }

    public void setGenericEntityFrom(GenericEntity genericEntityFrom) {
        this.genericEntityFrom = genericEntityFrom;
    }

    public GenericEntity getGenericEntityTo() {
        return genericEntityTo;
    }

    public void setGenericEntityTo(GenericEntity genericEntityTo) {
        this.genericEntityTo = genericEntityTo;
    }

    public double getSimilaritysProduct() {
        return similaritysProduct;
    }

    public void setSimilaritysProduct(double similaritysProduct) {
        this.similaritysProduct = similaritysProduct;
    }

    private double getSimilarity(GenericEntity genericEntityFrom, GenericEntity genericEntityTo) {
        AnalisadorTextual analisadorTextual = new AnalisadorTextual();
        TagMagnitudeVector geTamVectorFrom;
        try {
            String body = ((News) genericEntityFrom).getBody().replaceAll("<+[A-Za-z0-9._%+-/\\\"\'= ]+>", " ");
            geTamVectorFrom = analisadorTextual.analisar(((News) genericEntityFrom).getTitle() + body);
        } catch (AnalisadorTextualException e) {
            geTamVectorFrom = null;
            e.printStackTrace(); // TODO: Tratar isso adequadamente.
        }

        TagMagnitudeVector gerTamVectorTo;
        try {
            String body = ((News) genericEntityTo).getBody().replaceAll("<+[A-Za-z0-9._%+-/\\\"\'= ]+>", " ");
            gerTamVectorTo = analisadorTextual.analisar(((News) genericEntityTo).getTitle() + body);
        } catch (AnalisadorTextualException e) {
            gerTamVectorTo = null;
            e.printStackTrace(); // TODO: Tratar isso adequadamente.
        }

        return geTamVectorFrom.dotProduct(gerTamVectorTo);
    }
}